package com.sz.youban.dt.service.allinpay.helpper;

import java.util.HashMap;
import java.util.Map;

import com.sz.youban.dt.service.utils.GO;
import com.sz.youban.dt.service.utils.ShaUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.Validator;
import com.sz.youban.dt.entity.allinpay.AllinpayProperty;
import com.sz.youban.dt.model.AllinpayReceiveRequest;
import com.sz.youban.dt.model.AppInvokeRequest;
import com.sz.youban.dt.model.AuthenticationRequest;
import com.sz.youban.tonglian.entity.common.TongLianAuthenticationParam;
import com.sz.youban.tonglian.entity.common.TongLianPayNecessayParam;
import com.sz.youban.tonglian.entity.common.TongLianPaySysParam;

/**
 * Created by liyang on 2017/6/15.
 */
public class AllinpayUtil 
{

    private final Logger log = LoggerFactory.getLogger(this.getClass());
	
    
    /**
     * 检验实时代收付接口的请求参数
     */
    public static boolean checkreceiveAndPayParam(AllinpayReceiveRequest allinpayReceiveRequest) {
        if(Validator.isNullOrEmpty(allinpayReceiveRequest) || Validator.isNullOrEmpty(allinpayReceiveRequest.getPhone())
                || Validator.isNullOrEmpty(allinpayReceiveRequest.getTrxCode()) || Validator.isNullOrEmpty(allinpayReceiveRequest.getAccountNo())
                || Validator.isNullOrEmpty(allinpayReceiveRequest.getAccountName()) || Validator.isNullOrEmpty(allinpayReceiveRequest.getAmount())
                || Validator.isNullOrEmpty(allinpayReceiveRequest.getCardId()) || Validator.isNullOrEmpty(allinpayReceiveRequest.getChannel())
                || Validator.isNullOrEmpty(allinpayReceiveRequest.getOrderNo()) || Validator.isNullOrEmpty(allinpayReceiveRequest.getInvokeUrl())){

            //必要参数不足，返回失败
            return false;
        }else{
            return true;
        }
    }

    /**
     * 检验三要素验证的请求参数
     */
    public static boolean checkAuthenticationParam(AuthenticationRequest authenticationRequest) {
        if(Validator.isNullOrEmpty(authenticationRequest) ||  Validator.isNullOrEmpty(authenticationRequest.getAccountNo())
                || Validator.isNullOrEmpty(authenticationRequest.getAccountName()) || Validator.isNullOrEmpty(authenticationRequest.getCardId())){
            //必要参数不足，返回失败
            return false;
        }else{
            return true;
        }
    }

    /**
     * 检验实时代收付接口的交易类型
     */
    public static boolean checkreceiveAndPayTrxCode(AllinpayReceiveRequest allinpayReceiveRequest) {
        if(!allinpayReceiveRequest.getTrxCode().equals("100011") && !allinpayReceiveRequest.getTrxCode().equals("100014")){
            return false;
        }else{
            return true;
        }
    }


    /**
     * 组装请求实时单笔代收付的 key 用于 keySHA 签名校验
     */
    public static String receiveBulidKey(AllinpayReceiveRequest allinpayReceiveRequest) {
        Map<String,String> paraMap = new HashMap<String,String>();
        paraMap.put("phone",allinpayReceiveRequest.getPhone());
        paraMap.put("accountNo", allinpayReceiveRequest.getAccountNo());
        paraMap.put("accountName", allinpayReceiveRequest.getAccountName());
        paraMap.put("cardId",allinpayReceiveRequest.getCardId());
        paraMap.put("amount",allinpayReceiveRequest.getAmount());
        paraMap.put("userId",allinpayReceiveRequest.getUserId());
        paraMap.put("phone",allinpayReceiveRequest.getPhone());
        paraMap.put("province",allinpayReceiveRequest.getProvince());
        paraMap.put("city",allinpayReceiveRequest.getCity());
        paraMap.put("trxCode",allinpayReceiveRequest.getTrxCode());
        paraMap.put("channel",allinpayReceiveRequest.getChannel());
        paraMap.put("orderNo",allinpayReceiveRequest.getOrderNo());
        paraMap.put("invokeUrl",allinpayReceiveRequest.getInvokeUrl());
        String key = ShaUtil.formatUrlMap(paraMap);
        return key;
    }

    /**
     * 组装回调app通知结果接口的 key 用于 keySHA 签名校验
     */
    public static String invokeBulidKey(AppInvokeRequest appInvokeRequest) {
        Map<String,String> paraMap = new HashMap<String,String>();
        paraMap.put("status", GO.string(appInvokeRequest.getSatatus()));
        paraMap.put("dealNo", appInvokeRequest.getDealNo());
        paraMap.put("orderNo", appInvokeRequest.getOrderNo());
        String key = ShaUtil.formatUrlMap(paraMap);
        return key;
    }
    /**
     * 组装三要素验证接口的 key 用于 keySHA 签名校验
     */
    public static String authenticationBulidKey(AuthenticationRequest authenticationRequest) {
        Map<String,String> paraMap = new HashMap<String,String>();
        paraMap.put("accountName",authenticationRequest.getAccountName());
        paraMap.put("accountNo", authenticationRequest.getAccountNo());
        paraMap.put("cardId", authenticationRequest.getCardId());
        paraMap.put("phone", authenticationRequest.getPhone());
        String key = ShaUtil.formatUrlMap(paraMap);
        return key;
    }

    /**
     * @param develop
     * @param txcode
     * @return
     * 
     * txcode:交易代码。100011是实时单笔代收，100014是实时单笔代付
     */
    public static TongLianPaySysParam getTongLianSystemObj(Boolean develop,String txcode)
    {
    	TongLianPaySysParam paySysParam = new TongLianPaySysParam();
    	if(null != develop && develop)
    	{
    		
    		paySysParam.setMerchantId(AllinpayProperty.MERCHANTID_TEST);
    		paySysParam.setUserName(AllinpayProperty.USERNAME_TEST);
    		paySysParam.setPassWord(AllinpayProperty.PASSWORD_TEST);
    		paySysParam.setPfxPassword(AllinpayProperty.PFXPASSWORD_TEST);
    		paySysParam.setPfxPath(AllinpayProperty.PFXPATH_TEST);
    		paySysParam.setTltcerPath(AllinpayProperty.TLTCERPATH_TEST);
    		paySysParam.setUrl(AllinpayProperty.TRANURL_TEST_PAY);
    	}
    	else
    	{
    		paySysParam.setUrl(AllinpayProperty.TRANURL_PROD_PAY);
    		if(AllinpayProperty.TRX_CODE_RECEIVE.equals(txcode))
    		{
    			paySysParam.setMerchantId(AllinpayProperty.MERCHANTID_PROD_RECEIVE);
        		paySysParam.setUserName(AllinpayProperty.USERNAME_PROD_RECEIVE);
        		paySysParam.setPassWord(AllinpayProperty.PASSWORD_PROD_RECEIVE);
        		paySysParam.setPfxPassword(AllinpayProperty.PFXPASSWORD_PROD_RECEIVE);
        		paySysParam.setPfxPath(AllinpayProperty.PFXPATH_PROD_RECEIVE);
        		paySysParam.setTltcerPath(AllinpayProperty.TLTCERPATH_PROD_RECEIVE);
    		}
    		else if(AllinpayProperty.TRX_CODE_PAY.equals(txcode))
    		{
    			paySysParam.setMerchantId(AllinpayProperty.MERCHANTID_PROD_PAY);
        		paySysParam.setUserName(AllinpayProperty.USERNAME_PROD_PAY);
        		paySysParam.setPassWord(AllinpayProperty.PASSWORD_PROD_PAY);
        		paySysParam.setPfxPassword(AllinpayProperty.PFXPASSWORD_PROD_PAY);
        		paySysParam.setPfxPath(AllinpayProperty.PFXPATH_PROD_PAY);
        		paySysParam.setTltcerPath(AllinpayProperty.TLTCERPATH_PROD_PAY);
    		}
    	}
    	
    	return paySysParam;
    }
    
    /**
     * @param develop:是否是开发环境
     * @param accountNo：银行卡好
     * @param accountName：持卡人姓名
     * @param amount：金额
     * @param bankCode：银行代码
     * @param city：城市
     * @param phone：手机号
     * @param province：省分
     * @param trxCode：交易代码
     * @param userId:用户id
     * @return
     */
    public static TongLianPayNecessayParam getTongLianNessaryParam(Boolean develop,String accountNo, String accountName, 
    		String amount, String bankCode, String city, String phone, String province, String trxCode, String userId)
    {
    	TongLianPayNecessayParam payNecessaryParam = new TongLianPayNecessayParam();
    	payNecessaryParam.setAccountName(accountName);
		payNecessaryParam.setAccountNo(accountNo);
		payNecessaryParam.setAmount(amount);
		payNecessaryParam.setBankCode(bankCode);
		payNecessaryParam.setCity(city);
//		payNecessaryParam.setPhone(phone);
		payNecessaryParam.setProvince(province);
		payNecessaryParam.setTrxCode(trxCode);
		payNecessaryParam.setUserId(userId);
    	if(null != develop && develop)
    	{
    		payNecessaryParam.setBusiCode(AllinpayProperty.BUSICODE_TEST);
    	}
    	else
    	{
    		String busiCode = "";
    		if(AllinpayProperty.TRX_CODE_RECEIVE.equals(trxCode))
    		{
    			busiCode = AllinpayProperty.BUSICODE_PROD_RECEIVE;
    		}
    		if(AllinpayProperty.TRX_CODE_PAY.equals(trxCode))
    		{
    			busiCode = AllinpayProperty.BUSICODE_PROD_PAY;
    		}
    		
    		payNecessaryParam.setBusiCode(busiCode);
    	}
    	return payNecessaryParam;
    }

    public static TongLianAuthenticationParam getTongLianAuthenticationObj(Boolean develop)
    {
        TongLianAuthenticationParam authenticationsParam = new TongLianAuthenticationParam();
        if(null != develop && develop)
        {
        	authenticationsParam.setMerchantNo(AllinpayProperty.MERCHANTNO_TEST);
        	authenticationsParam.setDealKey(AllinpayProperty.DEALKEY_TEST);
        	authenticationsParam.setUrl(AllinpayProperty.TRANURL_TEST_VERIFY);
        }else{
            authenticationsParam.setMerchantNo(AllinpayProperty.MERCHANTNO_PROD);
            authenticationsParam.setDealKey(AllinpayProperty.DEALKEY_PROD);
            authenticationsParam.setUrl(AllinpayProperty.TRANURL_PROD_VERIFY);
        }

        return authenticationsParam;
    }
}
